package ImageDatabaseLoader;



import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class FaceImage {
	
	int width;
	int height;
	int maxPixelVal;
	BufferedImage image;
	String name;
	
	
	public FaceImage(String fileName) throws Exception
	{
		loadFromFile(fileName);
	}
	
	public String getName()
	{
		return name;
	}
	
	public double[] getImageAsDoubleArray() throws Exception
	{
		int imgWidth = image.getWidth();
		int imgHeight = image.getHeight();
		int k = 0;
		double[] imageFloats = new double[imgWidth * imgHeight];
		for (int x = 0; x < image.getWidth(); x++)
		{
			for (int y = 0; y < image.getHeight(); y++)
			{
				int rgbValue = image.getRGB(x, y);
				Color color =  new Color(rgbValue);
				
				// this code expects monocromatic colors
				if (color.getBlue() - color.getRed() != color.getGreen() - color.getRed())
				{
					throw new Exception("Invalid color: not grayScale");
				}
				imageFloats[k++] = color.getBlue()/255.0;
				
			}
		}
		return imageFloats;
	}
	
	private void loadFromFile(String fileName) throws Exception
	{
		File test = new File(fileName);
		image = ImageIO.read(test);
		if (image == null)
		{
			throw new Exception("Error loading image (" + fileName + ")");
		}
	}
	
	
}
